package com.medata.filter;

import com.medata.dataObject.BlackListForIp;
import com.medata.repository.BlackListForIpRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author ：zb
 * @date ：Created in 2019/3/26 0026 11:33
 * @description： 限制一定时间内访问次数,将违规用户ip拉黑功能
 */
@WebFilter(urlPatterns = "/login/*")
public class LimitCallFilter implements Filter {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private BlackListForIpRepository blackListForIpRepository;

    @Value("${LimitCall}")
    private Integer limitTimes;
    @Value("${LimitTime}")
    private Integer limitTime;
    @Value("${BlackCallTimes}")
    private Integer BlackCallTimes;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        String ip = servletRequest.getRemoteAddr();
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        String CallTimes = ops.get(ip);
        String s = ops.get(ip + "5");
        if (CallTimes!=null){
            Integer times=Integer.valueOf(CallTimes);
            Integer ss=s==null?0:Integer.valueOf(s);
            if (times<100&&ss<6){
                times++;
                String nowCallTime=String.valueOf(times);
                ops.set(ip,nowCallTime,limitTime, TimeUnit.SECONDS);
                filterChain.doFilter(servletRequest,servletResponse);
            }else {
                if (ss>5){
                    HttpServletResponse response=(HttpServletResponse)servletResponse;
                    response.sendRedirect("403");
//                    servletResponse.setCharacterEncoding("GBK");
//                    PrintWriter out = servletResponse.getWriter();
//                    out.println("<b>密码错误次数太多,请稍后访问</b>");
                }else {
                    if (times<BlackCallTimes) {
                        servletResponse.setCharacterEncoding("GBK");
                        PrintWriter out = servletResponse.getWriter();
                        out.println("<b>您的访问频率太高,歇一会吧</b>");
                    }else {
                        BlackListForIp blackListForIp=new BlackListForIp();
                        blackListForIp.setBlackIp(ip);
                        blackListForIp.setBlackTime(new Date());
                        blackListForIpRepository.save(blackListForIp);
                        servletResponse.setCharacterEncoding("GBK");
                        PrintWriter out = servletResponse.getWriter();
                        out.println("<b>你的ip地址被禁止访问</b>");
                    }
                }
            }
        }else {
                ops.set(ip,"1",limitTime, TimeUnit.SECONDS);
                filterChain.doFilter(servletRequest,servletResponse);
        }
    }
    @Override
    public void destroy() {

    }
}
